/*
 * Copyright (c) 2011 Obix Labs Limited
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 		Redistribution of source code must retain the above 
 * 		copyright notice, this list of conditions and the 
 * 		following disclaimer.
 *
 * 		Redistribution in binary form must reproduce the 
 * 		above copyright notice, this list of conditions 
 * 		and the following disclaimer in the documentation 
 * 		and/or other materials provided with the distribution.
 * 
 * 	THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF 
 * 	ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS 
 * 	AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
 * 	FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, 
 * 	ARE HEREBY EXCLUDED. OBIX LABS LIMITED ("Obix Labs") AND ITS 
 * 	LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE 
 * 	AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR 
 * 	ITS DERIVATIVES. IN NO EVENT WILL Obix Labs OR ITS LICENSORS BE 
 * 	LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 
 * 	INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE 
 * 	DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF 
 * 	LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS 
 * 	SOFTWARE, EVEN IF Obix Labs HAS BEEN ADVISED OF THE POSSIBILITY OF 
 * 	SUCH DAMAGES.
 */

package com.obixlabs.commons.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * <p>
 * Provides a set of utility methods for closing database resources {@link Statement}s,
 * {@link ResultSet}s and {@link Connection}s.
 * </p>
 * 
 *  <p>
 * The aim of this class is to reduce the quantity of code that is often necessitated by the 
 * logic required in <b>finally</b> blocks. Specifically the logic required in: trapping 
 * errors that can be generated when closing resources; and also testing for null resource 
 * references.
 * </p>
 * 
 * <p>
 * The following example demonstrates a typical scenario where method of this class
 * might be used.
 * <pre>
 * 	Connection dbConn=null;
 * 	Statement dbStatement=null;
 * 	try
 * 	{
 * 		//do something
 * 	}
 * 	finally
 * 	{
 * 		//note that either dbStatement or dbConn
 * 		//can still be null at this point
 * 		DBResourceCloser.close(dbStatement);
 * 		DBResourceCloser.close(dbConn);
 * 	}
 * </pre>
 * 
 * </p>
 * 
 * <p>
 * <b>Note:</b> All the methods in this class accept <code>null</code> arguments.
 * </p>
 */
public final class DBResourceCloser
{
        /**
         * <p>
         * Private default constructor to prevent accidental initialisation.
         * </p>
         */
        private DBResourceCloser(){}
        
	/**
	 * <p>
	 * Closes a {@link Statement}, trapping any errors that may occur as a result.  
	 * </p>
	 * 
	 * @param statement The {@link Statement} to close. 
	 */
	public static void close(Statement statement)
	{
		try{
			if (statement != null) statement.close();
		}catch (Throwable exception){}
	}// end method def

	/**
	 * <p>
	 * Closes a {@link ResultSet}, trapping any errors that may occur as a result.
	 * </p> 
	 * 
	 * @param resultSet The {@link ResultSet} to close. 
	 */	
	public static void close(ResultSet resultSet)
	{
		try{
			if (resultSet != null) resultSet.close();
		}catch (Throwable exception){}
	}// end method def

	/**
	 * <p>
	 * Closes a {@link Connection}, trapping any errors that may occur as a result.
	 * </p>
	 * @param connection The {@link Connection} to close. 
	 */		
	public static void close(Connection connection)
	{
		try{
			if (connection != null) connection.close();
		}catch (Throwable exception){}
	}// end method def
}
